//
//  HMWelcomeViewController.swift
//  我的微博
//
//  Created by 杨庆 on 15/10/17.
//  Copyright © 2015年 双元班. All rights reserved.
//

import UIKit
import SDWebImage
class HMWelcomeViewController: UIViewController {
    /// 图像的底边约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        //加载用户的头像
        if let urlstring = UserAccount.sharedAccount!.avatar_large{
                iconView.sd_setImageWithURL(NSURL(string: urlstring)!)
        }
    }
    /// 添加动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //提示: 这样的修改的约束,不会立即的生效,只是做了一个标记,统一由自动布局的刷新来实现
        iconBottomCons?.constant = UIScreen.mainScreen().bounds.height + (iconBottomCons?.constant)!
        //做动画来实现的
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //强制的更新约束
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            //发送通知,切换控制器
            NSNotificationCenter.defaultCenter().postNotificationName(HMRootViewControllerSwitchNotification, object: true)
        }
    }
    /// 添加控件到视图
    private func prepareUI(){
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        //自动布局
        //1.背景图片
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView":backImageView]))
        //2.头像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant:-160))
        //记录底边的约束(拿到底边的约束)
        iconBottomCons = view.constraints.last
        //3.设置标签
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant:16))
    }
    //MARK:- 懒加载控件;
    //背景图片
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //用户头像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置为圆的头像
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    }()
    /// 消息文字
    private lazy var label: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        return label
    }()
}
